<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" align-center>
            <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
                <el-form-item label="工厂货号" prop="goods_id">
                    <el-select v-model="form.goods_id" clearable style="width: 100%" placeholder="请选择" filterable>
                        <el-option v-for="item in factoryList" :key="item.id" :label="item.factory_number" :value="item.id" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="visible = false">取 消</el-button>
                <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
            </template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增生产单',
					edit: '编辑生产单',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
                    goods_id: "",
				},
                factoryList:[]
			}
		},
		mounted() {
            this.getFactory()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
                console.log('this.form :>> ', this.form);
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
                        res = await this.$API.produce.getProduce.add.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
            // 获取工厂货号
            async getFactory(){
                var res = await this.$API.all.goodsNumber.get();
                let array = []
                res.data.map(item=>{
                    array.push({
                        factory_number : item.factory_number + " + " + "("+item.colour+")",
                        id : item.id
                    })
                })
                this.factoryList = array
            },
		}
	}
</script>

<style lang="scss"></style>
